# To change this template, choose Tools | Templates
# and open the template in the editor.

class GeneralNewsLettersMaterial < ActiveRecord::Base
  belongs_to :material, :class_name => "Material"
  belongs_to :general_news_letter, :class_name => "GeneralNewsLetter"
  validates_uniqueness_of :material_id, :scope => :general_news_letter_id

  def self.search(general_news_letter_id, search_title, page)
    items_per_page = count_record_per_page :general_news_letters_materials
    order = "order_list"
    conditions_clauses = []
    conditions_options = []

    conditions_clauses << "general_news_letter_id = ?"
    conditions_options << general_news_letter_id


    if !search_title.nil? && search_title.size != 0
      conditions_clauses << "upper(materials.title) like upper(?)"
      conditions_options << "%#{search_title}%"
    end

    conditions = [conditions_clauses.join(" AND "), conditions_options].flatten
    general_news_letters_materials = GeneralNewsLettersMaterial.paginate :per_page => items_per_page,:page => page, :include => :material, :conditions => conditions, :order => order
    return general_news_letters_materials
  end
end
